import urllib.request

# https需要反爬，请求对象的定制是解决反爬的第一种手段
url = 'https://www.baidu.com'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}

# 因为urlopen方法中，不能接收字典，所以headers不能传递进去
# 所以需要定制请求对象
req = urllib.request.Request(url, None, headers)
# req = urllib.request.Request(url=url, headers=headers)

rep = urllib.request.urlopen(req)
print(rep.read().decode('utf-8'))
